<?php

/**
 * This is the model class for table "{{slider}}".
 *
 * The followings are the available columns in table '{{slider}}':
 * @property integer $id
 * @property string $url
 * @property integer $pro_id
 * @property string $caption
 * @property string $label
 * @property integer $del_flg
 *
 * The followings are the available model relations:
 * @property Products $pro
 */
class Slider extends CActiveRecord
{
	public $sliderUpload;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{slider}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('del_flg', 'numerical', 'integerOnly'=>true),
			array('caption, label', 'length', 'max'=>255),
			array('sliderUpload', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => true,'message'=>'Invalid','on'=>'create'),
			array('url', 'file', 'types' => 'jpg, gif, png','message'=>'Hình ảnh không được phép rỗng','on'=>'create'),
			// The following rule is used by search().
			array('id, caption, label, del_flg', 'safe', 'on'=>'search'),
		);
	}
	protected function beforeSave()	{
		$this->sliderUpload = CUploadedFile::getInstance($this, 'url');
		if (isset($this->sliderUpload)) {
			$fileName     = $this->sliderUpload->name;
			$uploadFolder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload/slider' . DIRECTORY_SEPARATOR . date('Y-m-d');
			if (!is_dir($uploadFolder)) {
				mkdir($uploadFolder, 0755, TRUE);
			}
			$uploadFile = $uploadFolder . DIRECTORY_SEPARATOR . $fileName;
			$this->sliderUpload->saveAs($uploadFile); //Upload file thong qua object CUploadedFile
			$this->url = '/upload/slider/' . date('Y-m-d') . '/' . $fileName; //Lưu path vào csdl
		}
		return parent::beforeSave();
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
// 			'pro' => array(self::BELONGS_TO, 'Products', 'pro_id'),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'url' => 'Hình ảnh',
			'caption' => 'Tiêu đề',
			'label' => 'Nhãn',
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('url',$this->url,true);
		//$criteria->compare('pro_id',$this->pro_id);
		$criteria->compare('caption',$this->caption,true);
		$criteria->compare('label',$this->label,true);
		$criteria->compare('del_flg',$this->del_flg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Slider the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
